package org.hard.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.apache.catalina.Executor;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.*;

/**
 * @author: 王林博
 * @date: 2022年10月15日 16:12
 */
@EnableScheduling
@Component
public class CronWorkController {

    // 添加定时任务
    @Scheduled(cron = "5/5 * * * * ?")
    public void doTask(){
        System.out.println(DateUtil.now() );
    }

    public static void main(String[] args) {
        CompletableFutureTest();
        test();
    }

//    ExecutorService executorService =  Executors.newFixedThreadPool(5);

    public static void test(){

        StopWatch stopWatch = new StopWatch("时间");
        stopWatch.start("dd");
        Map<String, Object> map = new HashMap<>();

        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        map.put("constellationResult", "15");

        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
            map.put("constellationResult", "12");

        stopWatch.stop();
        System.out.println(JSONUtil.toJsonStr(map));
        System.out.println(stopWatch.prettyPrint(TimeUnit.SECONDS));
    }

   public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 100, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(1000));

    public static void CompletableFutureTest() {


        StopWatch stopWatch = new StopWatch("时间");
        stopWatch.start("dd");
        Map<String, Object> map = new HashMap<>();

        // 1、查询二十四节气
//        CompletableFuture<String> twentyFourQuery = CompletableFuture.supplyAsync(()->{
//            try {
//                TimeUnit.MILLISECONDS.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "12";
//        },threadPoolExecutor);

        CompletableFuture<String> twentyFourQuery = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "12";
        });
        twentyFourQuery.thenAccept((result) -> {
            map.put("twentyFourResult", result);
        }).exceptionally((e) -> {
            map.put("twentyFourResult", "");
            return null;
        });

        // 2、查询星座
        CompletableFuture<String> constellationQuery = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.MILLISECONDS.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "15";
        });
        constellationQuery.thenAccept((result) -> {
            map.put("constellationResult", result);
        }).exceptionally((e) -> {
            map.put("constellationResult", "");
            return null;
        });

        // 3、allOf-两个查询必须都完成
        CompletableFuture<Void> allQuery = CompletableFuture.allOf(twentyFourQuery, constellationQuery);
        CompletableFuture<Map<String, Object>> future = allQuery.thenApply((result) -> {
            return map;
        }).exceptionally((e) -> {
            return null;
        });

        // 获取异步方法返回值
        // get()-内部抛出了异常需手动处理; join()-内部处理了异常无需手动处理，点进去一看便知。
        future.join();
        stopWatch.stop();
        System.out.println(JSONUtil.toJsonStr(map));
        System.out.println(stopWatch.prettyPrint(TimeUnit.SECONDS));
//        System.out.println(threadPoolExecutor.getActiveCount());
//        System.out.println(threadPoolExecutor.getCorePoolSize());
//        try {
//            TimeUnit.MILLISECONDS.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        // 使用ThreadPoolExecutor 会导致子线程不释放，主线程不停止
//        threadPoolExecutor.shutdownNow();
    }
}
